package com.seth.myapplication.adapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.seth.myapplication.AcceptActivity;
import com.seth.myapplication.R;
import com.seth.myapplication.bean.Rewords;
import com.seth.myapplication.bean.TaskType;
import com.seth.myapplication.bean.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Seth on 2017/10/10.
 */

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemHolder> {

    private Context mContext;
    private ArrayList<Rewords> mList;
    private LayoutInflater inflater;
    private int mFlag;

    public ItemAdapter(Context context){
        mContext = context;
        inflater = LayoutInflater.from(context);
        mList = new ArrayList<>();
    }

    public void setList(ArrayList<Rewords> list) {
        mList.clear();
        mList = list;
    }

    @Override
    public ItemAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View view =  inflater.inflate(R.layout.item_recommend,parent,false);


        return new ItemAdapter.ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemAdapter.ItemHolder holder, int position) {
        String msg = mList.get(position).getTaskTitle();
        holder.bindText(msg,position);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, int position, List<Object> payloads) {
        if(payloads.isEmpty()){
            onBindViewHolder(holder, position);
        }else{
            //注意：payloads的size总是1

            Rewords rewords = mList.get(position);
            ItemHolder viewHolder = holder;
            if(rewords.getSeter().getBitmap()!=null)
                holder.userFrog.setImageBitmap(rewords.getSeter().getBitmap());
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setFlag(int flag) {
        mFlag = flag;
    }

    public class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mTextView;
        private TextView taskPrice;
        private TextView taskTime;
        private TextView taskType;
        private TextView taskUsrname;
        private ImageView usrSex;
        private ImageView userFrog;


        private int itemClicked;
        public ItemHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.item_task_content);
            taskPrice = (TextView) itemView.findViewById(R.id.item_task_price);
            taskTime = (TextView) itemView.findViewById(R.id.item_last_time);
            taskType = (TextView) itemView.findViewById(R.id.item_task_type);
            taskUsrname = (TextView) itemView.findViewById(R.id.item_usr_name);
            usrSex = (ImageView) itemView.findViewById(R.id.item_usr_sex);
            userFrog = (ImageView) itemView.findViewById(R.id.item_usr_nick);
            itemView.setOnClickListener(this);

        }
        public void bindText(String mtext, int position){
            mTextView.setText(mtext);
            itemClicked = position;
            taskPrice.setText(mList.get(position).getPrice()+"");
            taskTime.setText(mList.get(position).getTaskDeadTime());
            taskType.setText(TaskType.getType(mList.get(position).getTaskType()));
            User user = mList.get(position).getSeter();
            if(user!=null){
                Log.i("UPDATE_REWORDS",user.getUsrId()+"");
                taskUsrname.setText(user.getNickname());
                if(user.getSex()==0){
                    usrSex.setImageResource(R.drawable.ic_boy);
                }else{
                    usrSex.setImageResource(R.drawable.ic_girl);
                }
            }
        }

        @Override
        public void onClick(View v) {
            Intent intent = AcceptActivity.newIntent(mContext, mList.get(itemClicked),mFlag);

            mContext.startActivity(intent);
        }
    }

    class UpdateFrogBroadCast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("result");
            int position=0;
            if ("image".equals(msg)) {
                int id = intent.getIntExtra("userId", 0);
                Bitmap bitmap = intent.getParcelableExtra("bitmap");
                Iterator<Rewords> i = mList.iterator();
                while (i.hasNext()){
                    Rewords  rewords = i.next();
                    position = mList.indexOf(rewords);
                    if(rewords.getUsrId()==id){
                        rewords.getSeter().setBitmap(bitmap);
                    }
                }
                notifyItemChanged(position,"update_frog");
            }

        }
    }

}
